<?php 
include "config.php";
$tos = $_POST['to'];
//echo $tos."<br>";
$to = explode (',',$tos);
//$from = $_POST['from'];
//$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$from.'+Indonesia&sensor=true');
$geocode=file_get_contents(json);
$output= json_decode($geocode);
$latitude1 = $output->results[0]->geometry->location->lat;
$longitude1 = $output->results[0]->geometry->location->lng;
//echo $latitude1 ."-". $longitude1."<br>";
$distancesawal = 100000000000000000000000000;
$result = mysql_query("SELECT id, lat, lng FROM `jalur`");

while($r = mysql_fetch_array($result)) 
   { 
	$latitude2 = $r[lat];
	$longitude2 = $r[lng];
	$id = $r[id];
	$theta = $longitude1 - $longitude2;
	$distance = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) +
	(cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) *
	cos(deg2rad($theta)));
	$distance = acos($distance);
	$distance = rad2deg($distance);
	$distance = $distance * 60 * 1.1515;
	$distances = $distance * 1609.344;
	//echo "id ".$id." jaraknya ".$distances." dimana distance awal ".$distancesawal." maka id fix ".$idfix."<br>";
		if ($distancesawal > $distances){
			$idfix = $id;
			$distancesawal = $distances ;
		}
		
	}
//	echo $idfix."<br>";


	
$latitude1 = $to[0];
$longitude1 = $to[1];
//echo $latitude1 ."-". $longitude1."<br>";	
$distancesawal = 100000000000000000000000000;
$result = mysql_query("SELECT id, lat, lng FROM `jalur`");
while($r = mysql_fetch_array($result)) 
   { 
	$latitude2 = $r[lat];
	$longitude2 = $r[lng];
	$id = $r[id];
	$theta = $longitude1 - $longitude2;
	$distance = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) +
	(cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) *
	cos(deg2rad($theta)));
	$distance = acos($distance);
	$distance = rad2deg($distance);
	$distance = $distance * 60 * 1.1515;
	$distances = $distance * 1609.344;
//echo "id ".$id." jaraknya ".$distances." dimana distance awal ".$distancesawal." maka id fix ".$idfixed."<br>";
		if ($distancesawal > $distances){
			$idfixed = $id;
			$distancesawal = $distances ;
		}
		
	}
//	echo $idfixed."<br>";

echo "<meta http-equiv='refresh' content='0;url=http://nusantaraview.web.id/dijkstra.php?a=$idfix&b=$idfixed'>";


?>